package by.demchenko.sample.util;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.Validate;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.logging.Logger;

public class SchemaNameAwareBasicDataSource extends BasicDataSource {

	private String schema;

	public String getSchema() {
		return schema;
	}

	public void setSchema(final String schema) {
		Validate.notEmpty(schema, "Illegal schema name");
		this.schema = schema;
	}

	@Override
	public Connection getConnection() throws SQLException {
		return super.getConnection();
	}

	@Override
	public Connection getConnection(final String username, final String password) throws SQLException {
		return super.getConnection(username, password);
	}

//	private Connection switchSchema(final Connection connection) throws SQLException {
//		final Statement stmt = connection.createStatement();
//		try {
//			stmt.execute("SET search_path TO " + schema);
//		} finally {
//			stmt.close();
//		}
///
//		return connection;
//	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return null;
	}
}
